package site.shihuan.helpdesk.service.controller;

import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import site.shihuan.helpdesk.common.model.JwtUser;
import site.shihuan.helpdesk.common.model.RestResult;
import site.shihuan.helpdesk.common.model.RestResultUtils;
import site.shihuan.helpdesk.security.context.UserContext;
import site.shihuan.helpdesk.service.manager.EsKnowledgeManager;
import site.shihuan.helpdesk.service.model.bo.KnowledgeSearchBO;
import site.shihuan.helpdesk.service.model.doc.EsKnowledge;

/**
 * 知识库API
 *
 * @author 周世焕
 * @Date 2021/3/13 17:21
 */
@RestController
@RequestMapping("/v1/service/knowledge")
public class KnowledgeController {

    @Autowired
    private EsKnowledgeManager esKnowledgeManager;

    /**
     * 查询知识库答案 ElasticSearch
     * @param tenantId
     * @param question
     * @return
     */
    @GetMapping("search")
    public RestResult searchKnowledge(@RequestParam String tenantId,@RequestParam String question) {
        return RestResultUtils.success(esKnowledgeManager.findAnswer(tenantId,question));
    }

    /**
     * 查询知识库
     * @return
     */
    @GetMapping("tenant")
    public RestResult findKnowledgeByTenantId(KnowledgeSearchBO searchBO) {
        searchBO.setTenantId(UserContext.getUser().getTenantId());
        return RestResultUtils.success(esKnowledgeManager.findKnowledgeByTenantId(searchBO));
    }

    /**
     * 查询知识库
     * @return
     */
    @GetMapping
    public RestResult findKnowledgeById(@RequestParam String id) {
        return RestResultUtils.success(esKnowledgeManager.findById(id));
    }

    @GetMapping("hot")
    public RestResult findHotList(@RequestParam String tenantId) {
        return RestResultUtils.success(esKnowledgeManager.findHotKnowledgeList(tenantId));
    }

    @PostMapping
    public RestResult saveKnowledge(@RequestBody EsKnowledge esKnowledge) {
        esKnowledge.setTenantId(UserContext.getUser().getTenantId());
        return RestResultUtils.success(esKnowledgeManager.saveKnowledge(esKnowledge));
    }

    @DeleteMapping
    public RestResult deleteKnowledge(@RequestParam String id) {
        esKnowledgeManager.deleteKnowledge(UserContext.getUser().getTenantId(),id);
        return RestResultUtils.success();
    }
}
